//
//  XHMyTeamTableViewCell.m
//  KuWoCan
//
//  Created by 周小宏 on 2017/5/3.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "XHMyTeamTableViewCell.h"
#import "MyTeamListModel.h"

@interface XHMyTeamTableViewCell ()

@property (nonatomic, strong) UIImageView *extendImageV;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *unfoldStatusButton;
@property (nonatomic, strong) UILabel *descriptionLabel;
@property (nonatomic, strong) UIButton *cargoButton;
@property (nonatomic, strong) UIButton *targetButton;
@property (nonatomic, strong) UIView *lineView;
@end

@implementation XHMyTeamTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    float indentPoints = self.indentationLevel * self.indentationWidth;

    self.contentView.frame = CGRectMake(
                                        indentPoints,
                                        self.contentView.frame.origin.y,
                                        self.contentView.frame.size.width - indentPoints,
                                        self.contentView.frame.size.height
                                        );
}

- (void)configCell {
    self.extendImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"juxing"]];
    [self.contentView addSubview:self.extendImageV];
    [self.extendImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(25 * screenW_6));
        make.left.equalTo(@(15 * screenW_6));
        make.size.mas_equalTo(CGSizeMake(10 * screenW_6, 11.5 * screenW_6));
    }];

    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = KwFont(15 * screenW_6);
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(17 * screenW_6));
        make.left.equalTo(self.extendImageV.mas_right).offset(19 * screenW_6);
        make.height.equalTo(@( 15 * screenW_6));
    }];

    if ([self.teamModel.totalPeopleNum integerValue] > 0) {
        self.unfoldStatusButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.unfoldStatusButton setImage:kImg(@"shouqi") forState:UIControlStateNormal];
        [self.unfoldStatusButton setImage:kImg(@"zhankai") forState:UIControlStateSelected];
        self.unfoldStatusButton.userInteractionEnabled = NO;
        [self.contentView addSubview:self.unfoldStatusButton];
        [self.unfoldStatusButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(18 * screenW_6);
            make.right.equalTo(self.contentView).offset(-16 * screenW_6);
            make.size.mas_equalTo(CGSizeMake(10 * screenW_6, 6 * screenW_6));
        }];
    }

    self.descriptionLabel = [UILabel new];
    self.descriptionLabel.font = KwFont(14 * screenW_6);
    [self.contentView addSubview:self.descriptionLabel];
    [self.descriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10 * screenW_6);
        make.left.equalTo(self.titleLabel);
        make.height.equalTo(@(14 * screenW_6));
    }];

    self.cargoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cargoButton.backgroundColor = KwBlueColor;
    self.cargoButton.tag = 10001;
    [self.cargoButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.cargoButton setTitle:@"进销存" forState:UIControlStateNormal];
    [self.cargoButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal | UIControlStateHighlighted];
    self.cargoButton.layer.cornerRadius = 3;
    self.cargoButton.layer.masksToBounds = YES;
    [self.contentView addSubview:self.cargoButton];
    [self.cargoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.descriptionLabel);
        make.top.equalTo(self.descriptionLabel.mas_bottom).offset(21 * screenW_6);
        if ([self.teamModel.userType integerValue] != 1) {
            make.size.mas_equalTo(CGSizeMake(0, 0));
        } else {
            make.size.mas_equalTo(CGSizeMake(87 * screenW_6, 31 * screenW_6));
        }
    }];

    self.targetButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.targetButton.backgroundColor = KwBlueColor;
    self.targetButton.tag = 10002;
    [self.targetButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.targetButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted | UIControlStateNormal];
    [self.targetButton setTitle:@"目标完成" forState:UIControlStateNormal];
    self.targetButton.layer.cornerRadius = 3;
    self.targetButton.layer.masksToBounds = YES;
    [self.contentView addSubview:self.targetButton];
    [self.targetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cargoButton.mas_right).offset(20 * screenW_6);
        make.top.equalTo(self.cargoButton);
        make.size.mas_equalTo(self.cargoButton);
    }];

    self.lineView = [[UIView alloc] init];
    self.lineView.backgroundColor = KwColor(@"ececec");
    [self.contentView addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.extendImageV);
        if ([self.teamModel.userType integerValue] != 1) {
            make.top.equalTo(self.descriptionLabel.mas_bottom).offset(20 * screenW_6);
        } else {
            make.top.equalTo(self.cargoButton.mas_bottom).offset(20 * screenW_6);
        }
        make.right.equalTo(self.contentView);
        make.height.equalTo(@1);
        make.bottom.equalTo(self.contentView);
    }];
}

- (void)hideExtemdImage {
    self.extendImageV.hidden = YES;
    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15 * screenW_6);
    }];
}

#pragma mark - Event Response
- (void)buttonClicked:(UIButton *)sender {
    switch (sender.tag) {
        case 10001:
            if (self.cargoButtonClicekd) {
                self.cargoButtonClicekd(self.teamModel);
            }
            break;

        case 10002:
            if (self.targetButtonClicked) {
                self.targetButtonClicked(self.teamModel);
            }
            break;
            
        default:
            break;
    }
}

#pragma mark - Private Method
- (void)setTitleLabelColorWithUserType:(NSInteger)userType {
    switch (userType)
    {
        case 1:
        {
            //经销商
            self.titleLabel.textColor = KwColor(@"55aee9");
        }
            break;
        case 2:
        {
            //区域主管
            self.titleLabel.textColor = KwColor(@"40ab5e");
        }
            break;
        case 3:
        {
            //区域经理
            self.titleLabel.textColor = KwColor(@"f1ab2e");
        }
            break;
        case 4:
        {
            //省去经理
            self.titleLabel.textColor = KwColor(@"f56812");
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - setter and getter
- (void)setUnfold:(BOOL)unfold {
    _unfold = unfold;
    self.unfoldStatusButton.selected = _unfold;
}

- (void)setTeamModel:(MyTeamListModel *)teamModel {
    _teamModel = teamModel;
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    [self configCell];
    self.titleLabel.text = [NSString stringWithFormat:@"%@ %@", teamModel.userArea, teamModel.userName];
    [self setTitleLabelColorWithUserType:[teamModel.userType integerValue]];
    self.descriptionLabel.text = [NSString stringWithFormat:@"销售：%@箱  回款：%@万", teamModel.salesTotalBox, teamModel.salesTotalVolume];
}

@end
